package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class HelpActivity extends AppCompatActivity {
    private MyReceiver receiver;
    private TextView tvHttp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        receiver = new MyReceiver();
        String action = "Help_Stitch";
        IntentFilter intentFilter = new IntentFilter(action);
        registerReceiver(receiver, intentFilter);
        Button btnHelp = (Button) findViewById(R.id.btnHelp);
        btnHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(action);
                sendBroadcast(intent);
            }
        });
        Button btnHttp = (Button) findViewById(R.id.btnHttp);
        btnHttp.setOnClickListener(view -> {
            new MyTask().execute();
        });
        tvHttp = (TextView) findViewById(R.id.tvHttp);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
    private class MyTask extends AsyncTask<Void, Void, Void> {
        String result;

        @Override
        protected void onPostExecute(Void aVoid) {
            tvHttp.setText(result);
            super.onPostExecute(aVoid);
        }

        @Override
        protected Void doInBackground(Void... voids) {  // 练习POST方式发送请求
            try {
                URL url = new URL("https://xx.ywu.cn/httpbin/post");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("POST");
                String data = "username=" + URLEncoder.encode("zhangsan", "UTF-8")
                        + "&password=123";
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Content-Length", data.length() + "");
                conn.setDoOutput(true);
                OutputStream os = conn.getOutputStream();
                os.write(data.getBytes());
                int code = conn.getResponseCode();
                if (code == 200) {
                    InputStream in = conn.getInputStream();
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    for (int length; (length = in.read(buffer)) != -1; ) {
                        out.write(buffer, 0, length);
                    }
                    result = out.toString("UTF-8");
                }
                conn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
                result = "网络连接失败";
            }
            return null;
        }
    }
}